package com.hqyj.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Author Hjy
 * @Description //TODO
 * @Date 21:11 2021/12/10
 * @Param
 * @return
 **/
public class DbUtil {
    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://127.0.0.1:3306/j21101?serverTimezone=Hongkong&useSSL=false&characterEncoding=utf-8&useUnicode=true";
    private static String user = "root";
    private static String password = "123456";

    private static Connection con;

    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private DbUtil() {
    }

    public static Connection getConnection() {
        try {
            if (con == null) {
                con = DriverManager.getConnection(url, user, password);
                return con;
            } else {
                if (con.isClosed()) {
                    con = DriverManager.getConnection(url, user, password);
                    return con;
                } else {
                    return con;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
